﻿using System.Diagnostics;
using System.Text;

namespace ChangeLog.CommunityToolkit
{
    public class GitRunner
    {
        public string ExecutablePath { get; }
        public string? WorkingDirectory { get; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="executablePath"></param>
        /// <param name="workingDirectory"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public GitRunner(string executablePath, string? workingDirectory = null)
        {
            ExecutablePath = executablePath ?? throw new ArgumentNullException(nameof(executablePath));
            WorkingDirectory = workingDirectory ?? Path.GetDirectoryName(executablePath);
        }

        public string Run(string arguments)
        {
            var info = new ProcessStartInfo(ExecutablePath, arguments)
            {
                CreateNoWindow = true,
                RedirectStandardOutput = true,
                UseShellExecute = false,
                WorkingDirectory = WorkingDirectory,
                StandardOutputEncoding = Encoding.UTF8,
            };
            var process = new Process
            {
                StartInfo = info,
            };
            //var p = Process.Start("cmd", "cd");
            
            //var o = p.StandardOutput.ReadToEnd();
            //Console.WriteLine($@"info.WorkingDirectory:{info.WorkingDirectory}");
            process.Start();
            return process.StandardOutput.ReadToEnd();
        }
    }
 
}
